package com.lz.ovuola.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lz.ovuola.entity.PhysiologicalReferencevDataDo;
import com.lz.ovuola.repository.mysql.impl.PhysiologicalReferencevDataRepository;
import com.lz.ovuola.service.PhysiologicalReferencevDataService;
import com.lz.ovuola.service.assembler.ReferencevDataAssembler;
import com.lz.ovuola.util.annotation.TargetDataSource;
import com.lz.ovuola.util.constant.DataSourceConsts;
import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.response.ResponseDataPackageUtils;

/**
 * 
 * @author fz
 *
 */
@Service("physiologicalReferencevDataService")
public class PhysiologicalReferencevDataServiceImpl implements
		PhysiologicalReferencevDataService {
	@Resource
	private PhysiologicalReferencevDataRepository physiologicalReferencevDataRepository;

	@TargetDataSource(DataSourceConsts.SECONDARY_DS)
	@Transactional(transactionManager = "transactionManagerDynamic", propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = true)
	@Override
	public String get(Integer userId) throws BusinessException {
		List<PhysiologicalReferencevDataDo> dataDos = physiologicalReferencevDataRepository
				.findByUserIdOrderByCreateTimeDesc(userId);
		if (!CollectionUtils.isEmpty(dataDos))
			return ResponseDataPackageUtils.success(ReferencevDataAssembler
					.getAssembler().toDto(dataDos.get(0)));
		return ResponseDataPackageUtils.success();
	}
}
